using Microsoft.AspNetCore.Mvc;
using MediPlatform.Read.Api.Models;
using MediPlatform.Read.Api.Services;
using MediPlatform.ErrorCode;

namespace MediPlatform.Read.Api.Controllers
{
    [ApiController]
    [Route("api/test/[controller]")]
    public class TestMedicalOrderWriteController : ControllerBase
    {
        private readonly MedicalOrderService _medicalOrderService;
        private readonly ILogger<TestMedicalOrderWriteController> _logger;

        public TestMedicalOrderWriteController(
            MedicalOrderService medicalOrderService,
            ILogger<TestMedicalOrderWriteController> logger)
        {
            _medicalOrderService = medicalOrderService;
            _logger = logger;
        }

        /// <summary>
        /// 测试订单发货
        /// </summary>
        [HttpPost("test-ship-order")]
        public async Task<ApiResult<object>> TestShipOrder([FromQuery] long orderId = 1)
        {
            try
            {
                var request = new ShipOrderRequest
                {
                    OrderId = orderId,
                    LogisticsCompany = "顺丰快递",
                    TrackingNumber = $"SF{DateTime.Now:yyyyMMdd}{orderId:D6}"
                };

                var result = await _medicalOrderService.ShipOrderAsync(request.OrderId, request.LogisticsCompany, request.TrackingNumber);

                if (result)
                {
                    return ApiResult<object>.Success(new { message = "测试订单发货成功", orderId, logisticsCompany = request.LogisticsCompany, trackingNumber = request.TrackingNumber });
                }
                else
                {
                    return ApiResult<object>.Error("测试订单发货失败，请检查订单状态");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "测试订单发货失败");
                return ApiResult<object>.Error("测试订单发货失败");
            }
        }

        /// <summary>
        /// 测试确认收货
        /// </summary>
        [HttpPost("test-confirm-receipt")]
        public async Task<ApiResult<object>> TestConfirmReceipt([FromQuery] long orderId = 1)
        {
            try
            {
                var result = await _medicalOrderService.ConfirmReceiptAsync(orderId);

                if (result)
                {
                    return ApiResult<object>.Success(new { message = "测试确认收货成功", orderId });
                }
                else
                {
                    return ApiResult<object>.Error("测试确认收货失败，请检查订单状态");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "测试确认收货失败");
                return ApiResult<object>.Error("测试确认收货失败");
            }
        }

        /// <summary>
        /// 测试获取物流公司列表
        /// </summary>
        [HttpGet("test-logistics-companies")]
        public ApiResult<List<LogisticsCompanyInfo>> TestGetLogisticsCompanies()
        {
            try
            {
                var companies = _medicalOrderService.GetLogisticsCompanies();
                return ApiResult<List<LogisticsCompanyInfo>>.Success(companies);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "测试获取物流公司列表失败");
                return ApiResult<List<LogisticsCompanyInfo>>.Error("测试获取物流公司列表失败");
            }
        }

        /// <summary>
        /// 测试获取物流追踪详情
        /// </summary>
        [HttpGet("test-logistics-tracking")]
        public ApiResult<LogisticsTrackingDetailResponse> TestGetLogisticsTrackingDetail([FromQuery] long orderId = 1)
        {
            try
            {
                var result = _medicalOrderService.GetLogisticsTrackingDetail(orderId);
                return ApiResult<LogisticsTrackingDetailResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "测试获取物流追踪详情失败");
                return ApiResult<LogisticsTrackingDetailResponse>.Error("测试获取物流追踪详情失败");
            }
        }

        /// <summary>
        /// 测试完整的状态流转
        /// </summary>
        [HttpPost("test-status-flow")]
        public async Task<ApiResult<object>> TestStatusFlow([FromQuery] long orderId = 1)
        {
            try
            {
                var results = new List<string>();

                // 1. 发货
                var shipResult = await _medicalOrderService.ShipOrderAsync(orderId, "顺丰快递", $"SF{DateTime.Now:yyyyMMdd}{orderId:D6}");
                results.Add($"发货: {(shipResult ? "成功" : "失败")}");

                // 2. 确认收货
                var receiptResult = await _medicalOrderService.ConfirmReceiptAsync(orderId);
                results.Add($"确认收货: {(receiptResult ? "成功" : "失败")}");

                return ApiResult<object>.Success(new { 
                    message = "状态流转测试完成", 
                    orderId, 
                    results 
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "测试状态流转失败");
                return ApiResult<object>.Error("测试状态流转失败");
            }
        }
    }
} 